/**
 * 
 */
package com.gametime.setup;

import java.util.ArrayList;

import com.gametime.model.Person;
import com.gametime.model.Player;
import com.gametime.model.Position;
import com.gametime.model.Role;
import com.gametime.model.Team;
import com.gametime.service.LeagueService;
import com.gametime.service.TeamService;
import com.gametime.util.ComUtils;
import com.gametime.util.LogUtil;


/**
 * @author dave
 *
 */
public class LeagueGenerator {

	/**
	 * @param args
	 */
	
	private ArrayList<Team> teams=null;
	
	private int seasonId;
	
	public LeagueGenerator() 
	throws Exception
	{
		// init teams
		teams = TeamService.getAllTeams();
		seasonId = LeagueService.getActiveSeason();
		LogUtil.trace("teams: " + teams.size());
	}
	
	public void generateTeam
	(
			int teamId
	) throws Exception
	{
	  /*
	   * Starters
	   * 1 : 1 or 2
	   * 2: 2,3,4
	   * 3: 4,5,6
	   * 4,5: 6,7,8,9
	   * 
	   * Roation
	   * 1: Small
	   * 2: mid
	   * 3: big
	   * 
	   * reserve
	   * 3 random positions
	   */
	  
	  
    Player player = generatePlayer
        (
            Position.POINT_GUARD_SEQ, 
            Position.COMBO_GUARD_SEQ, 
            Role.STARTER
        );
    
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.COMBO_GUARD_SEQ, 
            Position.WING_SEQ, 
            Role.STARTER
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.WING_SEQ, 
            Position.FORWARD_SEQ, 
            Role.STARTER
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.FORWARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.STARTER
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.FORWARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.STARTER
        );
    
    savePlayer(player, teamId);
    
    /************* rotation *****************/
    player = generatePlayer
        (
            Position.POINT_GUARD_SEQ, 
            Position.BIG_GUARD_SEQ, 
            Role.ROTATION
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.BIG_GUARD_SEQ, 
            Position.FORWARD_SEQ, 
            Role.ROTATION
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.FORWARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.ROTATION
        );
    
    savePlayer(player, teamId);
    
    /************* reserves *****************/
    player = generatePlayer
        (
            Position.POINT_GUARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.RESERVE
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.POINT_GUARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.RESERVE
        );
    
    savePlayer(player, teamId);
    
    player = generatePlayer
        (
            Position.POINT_GUARD_SEQ, 
            Position.CENTER_SEQ, 
            Role.RESERVE
        );
    
    savePlayer(player, teamId);
	}
	
	private Player generatePlayer(int minSeq, int maxSeq, String role)
	throws Exception
	{
	  PlayerGenerator playerGenerator = PlayerGenerator.getInstance();
    PositionGenerator posGenerator = PositionGenerator.getInstance();
	  int randomNumber = ComUtils.getRandomInt
        (
            minSeq, 
            maxSeq
        );
	  
	  Position position = posGenerator.getMappedPostion(randomNumber);
    Person person = playerGenerator.generatePerson();
    return Player.generatePlayerRandomly(person, position, role);
	}
	
	private void savePlayer(Player player, int teamId)
	throws Exception
	{
	  player.save();
	  LogUtil.trace(player);
    player.saveTeam(teamId, LeagueService.getActiveSeason());
	}
	
	
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		LeagueGenerator generator = new LeagueGenerator();
		ArrayList<Team> teams = TeamService.getAllTeams();
		for(Team t : teams)
		{
		  generator.generateTeam(t.getTeamId());
		}
		
		
	}

}
